// 用户管理
var commonView = require("./commonView.js");
var $manageDetails, $manageModal, $savepwdValidform;
var neworchange; // 1:新增 2：修改
// 取用户表数据
var editUserId;
var yhdata; // 当前编辑的用户数据

function getUserTableData() {
    var institution = $.cookie('institution');
    var name = $("#user-name").val();
    var userPhone = $("#user-manage_phone").val();
    var paraments = {
        institution: institution,
        userName: name,
        moblNo: userPhone
    };

    $.post(userTableServlet, paraments, createUserTable);

}

// 当天记录回调函数
function createUserTable(tRecordData) {
    // console.dir(tRecordData);
    commonView.needLoading(tRecordData);
    var trHtml = '';
    if (tRecordData.data && tRecordData.data.length > 0) {
        var RData = tRecordData.data;

        for (var i = 0, l = RData.length; i < l; i++) {
            var labelClass = "important";
            
            var roleName = RData[i].roleId == "0" ? "管理员" : "员工";
            var phone = "";
            if (RData[i].moblNo != "null" && RData[i].moblNo != null) {
                phone = RData[i].moblNo;
            }
            var theDate = new Date(RData[i].insertime);
            var theDateformate = commonView.getDateFormate("yyyy-mm-dd hh:ii:ss", theDate);
            var statusMsg = "停用";
            if (RData[i].status == "1") {
                statusMsg = "正常";
                labelClass = "success";
            }
            trHtml += `<tr><td>${i+1}</td><td>${RData[i].userName}</td><td>${roleName}</td><td>${phone}</td><td>${theDateformate}</td><td><span class="label label-${labelClass}">${statusMsg}</span></td>`;
            if(RData[i].logonName.search(/^admin/) == -1){
                trHtml += `<td><div class=""><a data-id="${RData[i].id}" class="btn label-primary btn-mini" href="javascript:;">编辑</a></div></td></tr>`;
            }else{
                trHtml += `<td></td></tr>`;
            }
            
        }
        $manageDetails.find("tbody").html(trHtml);
        $manageDetails.find("a.btn").on("click", editUser);
    } else {
        $manageDetails.find("tbody").html("<tr><td><p>没有查询记录</p></td></tr>");
    }

}

// 编辑用户
function editUser() {
    editUserId = $(this).data("id");
    $.post(managshow, {
        id: editUserId
    }, function(ydata) {

        commonView.needLoading(ydata);
        if (ydata.code == "0000") {
            //        $("#user-num").val(ydata.data.userId);
            $("#new-user_name").val(ydata.data.userName);
            $("#mange-phone").val(ydata.data.moblNo);
            neworchange = 2;
            if(ydata.data.roleId == "1"){
                $("#roleId-select").html('<option value="1" selected="true">员工</option><option value="0">管理员</option>');
            }else{
                $("#roleId-select").html('<option value="1">员工</option><option  selected="true" value="0">管理员</option>');
            }
            if(ydata.data.status == "1"){
                $("#status-select").html('<option value="1" selected="true">正常</option><option value="2">停用</option>');
            }else{
                $("#status-select").html('<option value="1">正常</option><option selected="true" value="2">停用</option>');
            }

            //        $("#user-num_box").show();
            $savepwdValidform.check();
            $("#rp-box").show();
            $manageModal.modal("show");

        }

    });

}

// 新增修改用户
function savepwduser() {
    var theQueryUrl = window.managsave;
    if ($savepwdValidform.check()) {
        var paraments = {
            id: "",
            userName: $("#new-user_name").val(),
            roleId: $("#roleId-select option:selected").val(),
            moblNo: $("#mange-phone").val(),
            logonName: $("#mange-phone").val(),
            status: $("#status-select option:selected").val()
        };
        if (neworchange == 2) {
            paraments.id = editUserId;
        }

        $.post(theQueryUrl, paraments, function(ebdata) {
            //            console.dir(ebdata);
            if (ebdata.code == "0000") {
                $manageModal.modal("hide");
                if (neworchange == 2) {
                    $("#promt-body").text("修改用户成功");
                } else {
                    $("#promt-body").text("新增用户成功");
                }
                getUserTableData();
            } else {
                if (neworchange == 2) {
                    $("#promt-body").text("修改用户失败");
                } else if (ebdata.code == "0114" && ebdata.msg && ebdata.msg != "null" && ebdata.msg !="undefined"){
                    $("#promt-body").text(ebdata.msg);
                } else{
                    $("#promt-body").text("新增用户失败");
                }
            }


            $("#prompt-modal").modal("show");
        });
    }

}


// 重置密码
function resetPassword() {
    $.post(managpwdServlet, {
        id: editUserId
    }, function(rpdata) {
        if (rpdata.code == "0000") {
            $("#promt-body").text("重置密码成功");
            $("#prompt-modal").modal('show');
        } else {
            $("#promt-body").text("重置密码失败");
            $("#prompt-modal").modal('show');
        }
    });
}

// 初始变量
function initManageValue() {
    $manageDetails = $("#manage-details");
    $manageModal = $("#manage-modal");
    $savepwdValidform = $("#savepwd-validform").Validform({
        tiptype: 2
    });
    $("#new-user").on("click", function() {
        neworchange = 1;
        $("#rp-box").hide();
        $("#new-user_name").val("");
        $("#mange-phone").val("");
        //    $("#user-num_box").hide();
        $savepwdValidform.check();
        $savepwdValidform.resetForm();
        $manageModal.modal("show");

    });
    $("#query-user").on("click", function() {
        getUserTableData();
    });
    $("#save-pwd_user").on("click", savepwduser);

    $("#reset-password").on("click", resetPassword);
}

module.exports = {
    getUserTableData: getUserTableData,
    initManageValue: initManageValue
};